// components/collapseItem/collapseItem.js
Component({
	options: {
		addGlobalClass: true,
		multipleSlots: true, // 在组件定义时的选项中启用多slot支持
	},
	relations: {
		'../collapse/collapse': {
			type: 'parent', // 关联的目标节点应为子节点
			linked: function (target) {
				// 每次有custom-li被插入时执行，target是该节点实例对象，触发在该节点attached生命周期之后
				// console.log(target)
				// console.log(target.data)
			},
			linkChanged: function (target) {
				// 每次有custom-li被移动后执行，target是该节点实例对象，触发在该节点moved生命周期之后
				// console.log(target)
			},
			unlinked: function (target) {
				// 每次有custom-li被移除时执行，target是该节点实例对象，触发在该节点detached生命周期之后
				// console.log(target)
			},
		},
	},
	/**
	 * 组件的属性列表
	 */
	properties: {
		value: { type: String, value: '' },
		open: { type: Boolean, value: false },
	},

	/**
	 * 组件的初始数据
	 */
	data: {
		node: null,
		height: 0,
	},

	ready() {
		var nodes = this.getRelationNodes('../collapse/collapse')
		let node = nodes[0]
		// console.log(node)
		this.setData({
			node,
		})
		this.judgeOpen()
		this.createSelectorQuery()
			.select('.collapse-item-inner')
			.boundingClientRect(rect => {
				this.setData({ height: rect.height })
			})
			.exec()
	},

	/**
	 * 组件的方法列表
	 */
	methods: {
		judgeOpen() {
			try {
				let node = this.data.node
				let arr = node.data.value.split(',')
				let open = arr.indexOf(this.data.value) > -1
				this.setData({
					open,
				})
			} catch (error) {}
		},
		clickFn() {
			let open = !this.data.open
			this.setData({
				open,
			})
			let node = this.data.node
			node.changeValue(this.data.value, open)
		},
	},
})
